package com.lovi.net_.udp;

import java.io.IOException;
import java.net.*;

/**
 * 作为发送端9998
 * 向接收端询问 “明天去吃火锅吗”
 * 接收接收端的回复“好啊，明天见！”
 */
public class UDPSender02 {
    public static void main(String[] args) throws IOException {
        //1. 向接收端询问 “明天去吃火锅吗”
        //发送端使用的端口为9998
        DatagramSocket socket = new DatagramSocket(9998);
        String sendStr = "明天去吃火锅吗";
        socket.send(new DatagramPacket(sendStr.getBytes(),sendStr.getBytes().length, InetAddress.getLocalHost(),9999));
        //2.接收接收端的回复“好啊，明天见！”
        System.out.println("发送端 等待接收回复...");
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
        socket.receive(packet);
        //输出接收到的数据！
        String s = new String(packet.getData(),0,packet.getLength());
        System.out.println("发送端 收到回复：" + s);
        socket.close();
        System.out.println("发送端退出。。。");

    }
}
